package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.Database;

public class Personlist {
	
	/*
	 * REGION private fields
	 */
	ArrayList<Person> persons;
	
	/*
	 * REGION public constructor
	 */
	public Personlist() {
		load();
	}
	
	/*
	 * REGION public getters
	 */
	public ArrayList<Person> getPersonlist() {
		return persons;
	}
	
	public int getPersonCount() {
		return persons.size();
	}
	
	/*
	 * REGION public methods
	 */
	public void load() {
		
		// Establish a connection to the database
		Database database = new Database();
		database.connectToDatabase();
		
		try {
			ResultSet rs = database.getPersonlist();
			
			persons = new ArrayList<Person>();
			
			while (rs.next()) {
				int personID = rs.getInt("ID");
				Person person = new Person(personID);
				persons.add(person);
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}	
		database.disconnectDatabase();
	}

}

